package com.cloudkinto.service.covershows.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.common.DateUtils;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.dao.NewsInformationDao;
import com.cloudkinto.entity.NewsInformationDo;
import com.cloudkinto.service.covershows.ThymeleafShowService;
import com.cloudkinto.service.covershows.vo.NewsInformationPageRes;
import com.cloudkinto.service.covershows.vo.NewsInformationReq;
import com.cloudkinto.service.file.FileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author fanp
 * @version v1.0.0
 * @Description :
 * @Create on : 2021/3/22 15:34
 **/
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class ThymeleafShowServiceImpl extends ServiceImpl<NewsInformationDao, NewsInformationDo> implements ThymeleafShowService {
    @Autowired
    private FileService fileService;
    @Autowired
    private NewsInformationDao newsInformationDao;

    @Override
    public List<NewsInformationPageRes> getPageList(Map<String, String> map) {
        List<NewsInformationPageRes> list = new ArrayList<>();
        IPage<NewsInformationDo> page = pageInit(map);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("create_time");
        page = this.page(page, wrapper);
        List<NewsInformationDo> newsInformationDos = page.getRecords();
        long pages = page.getPages();
        newsInformationDos.forEach(newsInformationDo -> {
            NewsInformationPageRes newsInformationPageRes = new NewsInformationPageRes();
            BeanUtils.copyProperties(newsInformationDo, newsInformationPageRes);
            newsInformationPageRes.setYear(TimeUtils.formatDate(newsInformationDo.getCreateTime(),"yyyy-MM"));
            newsInformationPageRes.setDay(TimeUtils.formatDate(newsInformationDo.getCreateTime(),"dd"));
            newsInformationPageRes.setFileUrl(fileService.getFilePath(newsInformationDo.getFileId()));
            newsInformationPageRes.setBrowseCount(String.valueOf(newsInformationDo.getBrowseCount()));
            //暂时这个字段无用 先把页数放进来了 用于分页
            newsInformationPageRes.setCollectCount(String.valueOf(pages));
            //利用正则修改
            newsInformationPageRes.setContent(newsInformationDo.getContent().replaceAll("\\&[a-zA-Z]{1,10};", "")
                    .replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", "")
                    .replaceAll("</[a-zA-Z]+[1-9]?>", ""));
            list.add(newsInformationPageRes);
        });
        return list;
    }

    private IPage<NewsInformationDo> pageInit(Map<String, String> map) {
        IPage<NewsInformationDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 5);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    //查看单个明细
    @Override
    public NewsInformationReq selectDetail(Integer id) {
        NewsInformationReq newsInformationReq = new NewsInformationReq();
        NewsInformationDo newsInformationDo = newsInformationDao.selectById(id);
        BeanUtils.copyProperties(newsInformationDo, newsInformationReq);
        newsInformationReq.setYear(TimeUtils.formatDate(newsInformationDo.getCreateTime(),"yyyy-MM"));
        newsInformationReq.setDay(TimeUtils.formatDate(newsInformationDo.getCreateTime(),"dd"));
        newsInformationReq.setFilePath(fileService.getFilePath(newsInformationDo.getFileId()));
        String outStr = newsInformationDo.getContent().replaceAll("\\&[a-zA-Z]{1,10};", "") //去除类似&lt; &gt; &nbsp;的字串
                .replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", "") //去除开始标签及没有结束标签的标签
                .replaceAll("</[a-zA-Z]+[1-9]?>", "");
        newsInformationReq.setContent(outStr);
        return newsInformationReq;
    }
}
















































